"""Tests for configuration management."""

import os
import tempfile
from pathlib import Path

import pytest
import yaml

from auto_review.config import ConfigManager, RepositoryConfig


def test_repository_config_from_dict():
    """Test RepositoryConfig creation from dictionary."""
    data = {
        "name": "test/repo",
        "priority": 1,
        "max_review_per_day": 10,
        "enabled": True,
        "ssh_url": "git@gitcode.com:test/repo.git",
        "local_path": "./repos/repo",
    }
    config = RepositoryConfig.from_dict(data)
    assert config.name == "test/repo"
    assert config.priority == 1
    assert config.max_review_per_day == 10
    assert config.enabled is True


def test_config_manager_load_config():
    """Test ConfigManager loading configuration."""
    # Create temporary config file
    config_data = {
        "gitcode": {
            "api_base_url": "https://api.gitcode.com",
            "access_token": "test_token",
        },
        "llm": {
            "provider": "dashscope",
            "api_key": "test_key",
            "base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1",
            "model": "qwen-plus",
            "temperature": 0.3,
            "max_tokens": 4000,
        },
        "repositories": [
            {
                "name": "test/repo",
                "priority": 1,
                "max_review_per_day": 10,
                "enabled": True,
                "ssh_url": "git@gitcode.com:test/repo.git",
                "local_path": "./repos/repo",
            }
        ],
        "email": {
            "smtp_server": "smtp.example.com",
            "smtp_port": 587,
            "smtp_user": "user@example.com",
            "smtp_password": "password",
            "from_address": "from@example.com",
            "to_addresses": ["to@example.com"],
        },
        "schedule": {"enabled": True, "time": "09:00", "timezone": "Asia/Shanghai"},
        "review": {
            "severity_levels": [
                {"name": "critical", "threshold": 0.8, "send_email": True}
            ]
        },
        "database": {"type": "sqlite", "path": "./data/review.db"},
        "logging": {"level": "INFO", "file": "./logs/review.log"},
    }

    with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f:
        yaml.dump(config_data, f)
        config_path = f.name

    try:
        config_manager = ConfigManager(config_path)
        repos = config_manager.get_repositories()
        assert len(repos) == 1
        assert repos[0].name == "test/repo"
    finally:
        os.unlink(config_path)

